<?php
/**
 * 获取产能数据及结构
 */
class Capacity extends CI_Model{
   //获取store_app_capacity_menu父目录数据
   public function get_capacity_menu($channel='')
   {
        $this->db->select("*");
		
		if($channel<>""){
			$this->db->where("channel",$channel);
		}
        $this->db->where("pmenuid",0);
        $this->db->order_by("orderid","ASC");
        $this->db->from("store_app_capacity_menu");
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
        return $query->result_array();    
   }
      
   
    //获取app_menu子目录数据
   public function get_capacity_submenu($pmenuid)
   {
        $this->db->select("*");
        $this->db->where("pmenuid",$pmenuid);
        $this->db->order_by("orderid","ASC");
        $this->db->from("store_app_capacity_menu");
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
        return $query->result_array();    
   }   
   
   //获取价格指数
   public function getindexnums($is_channel)
   {
        $this->db->select("*");
        $this->db->where($is_channel,1);
        $this->db->order_by("inputdate","DESC");
		$this->db->limit(2);
        $this->db->from("store_app_indexnums");
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
        return $query->result_array();    
   
   }
   
    //获取产能数据
   public function getcapacitydata($is_site,$catid)
   {
        $this->db->select("catid,pname as title,ptype,punit as unit,pdata as nums,pdate as date");
        $this->db->where($is_site,1);
		$this->db->where("catid",$catid);
		$this->db->order_by("porder","ASC");
		$this->db->limit(3);
        $this->db->from("store_app_capacity_tb");
        $query = $this->db->get();
        if($query->num_rows() < 1)
            return false;
		$rearry = array();	
        $rearry = $query->result_array();
		//处理数据
		for($i=0;$i<count($rearry);$i++){
		
			if($rearry[$i]['nums'] == ceil($rearry[$i]['nums'])){
				$rearry[$i]['nums'] = ceil($rearry[$i]['nums']);
			}
			//处理日期
			if($rearry[$i]['ptype']==2){
				//取月
				$rearry[$i]['date'] = date('Y-n',strtotime($rearry[$i]['date']));
			}
			if($rearry[$i]['ptype']==3){
				//取年
				$rearry[$i]['date'] = date('Y',strtotime($rearry[$i]['date']));
			}
		
		}
		
   		return $rearry;
   }
   
   //获取产能走势图数据
   public function getcapacitychartdata($channel,$catid,$no,$mydate)
   {
       /*
	    $this->db->select("catid,punit,price,inputdate");
        $this->db->where("channel",$channel);
		$this->db->where("catid",$catid);
		$this->db->order_by("inputdate","ASC");
		$this->db->limit(90);
        $this->db->from("store_app_capacity_data");
		*/
		if($channel == 'dlg'){
			$begindate = date("Y-m-d",strtotime("-6 month",strtotime($mydate)));
		}else{
			$begindate = date("Y-m-d",strtotime("-10 month",strtotime($mydate)));
		}
		
		$sql = "select catid,kname,punit,price,inputdate 
				from store_app_capacity_data 
				where catid='{$catid}' and channel='{$channel}' and (inputdate>'{$begindate}'  and inputdate<'{$mydate}')
				order by inputdate ASC ";
		
        $query = $this->db->query($sql);
        if($query->num_rows() < 1)
            return false;
		$rearry = array();
		$data = array();

        $rearry = $query->result_array();   
		for($i=0;$i<count($rearry);$i++){
			$data['x'][] = $rearry[$i]['inputdate'];
			$data[$no][] = $rearry[$i]['price'];
			$data['unit'] = $rearry[$i]['punit'];
			$data['yname'] = $rearry[$i]['kname'];
		}
		return $data;
   }
  
}